package com.chuangke.lowcode;

import com.chuangke.lowcode.component.delete.BillDeleteComponent;
import com.chuangke.lowcode.component.delete.DeleteComponent;
import com.chuangke.lowcode.component.flow.FlowIdQueryComponent;
import com.chuangke.lowcode.component.flow.FlowStartComponent;
import com.chuangke.lowcode.component.handle.*;
import com.chuangke.lowcode.component.query.BillLoadComponent;
import com.chuangke.lowcode.component.query.PageQueryComponent;
import com.chuangke.lowcode.component.query.QueryComponent;
import com.chuangke.lowcode.component.save.SaveComponent;
import com.chuangke.lowcode.component.save.SaveOrUpdateComponent;
import com.chuangke.lowcode.component.update.UpdateComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@Transactional(rollbackFor = Throwable.class)
public class LowCodeExecutor {

    @Autowired
    private BeforeSaveHandleDataComponent dataPreprocessingComponent;
    @Autowired
    private BeforeSaveHandleDataComponent beforeSaveHandleDataComponent;
    @Autowired
    private UpdateComponent updateComponent;
    @Autowired
    private SaveComponent saveComponent;
    @Autowired
    private DeleteComponent deleteComponent;
    @Autowired
    private BillDeleteComponent billDeleteComponent;
    @Autowired
    private BillLoadComponent billLoadComponent;
    @Autowired
    private FlowIdQueryComponent flowIdQueryComponent;
    @Autowired
    private FlowStartComponent flowStartComponent;
    @Autowired
    private BeforeSaveBillHandleBillColumnValueComponent beforeSaveBillHandleBillColumnValueComponent;
    @Autowired
    private BeforeUpdateValidateStatusComponent beforeUpdateValidateStatusComponent ;
    @Autowired
    private SaveOrUpdateComponent saveOrUpdateComponent;

    @Autowired
    private BeforeActionHandlerQueryWrapperComponent beforeActionHandlerQueryWrapperComponent;
    @Autowired
    private PageQueryComponent pageQueryComponent ;
    @Autowired
    private QueryComponent queryComponent ;

    /**
     * 单据保存
     * @param context
     */
    public void saveBill(LowCodeContext context) {
        beforeSaveHandleDataComponent.process(context);
        beforeSaveBillHandleBillColumnValueComponent.process(context);
        saveOrUpdateComponent.process(context);
        flowIdQueryComponent.process(context);
        flowStartComponent.process(context);
    }

    /**
     * 单据修改
     * @param context
     */
    public void updateBill(LowCodeContext context) {
    	beforeUpdateValidateStatusComponent.process(context);
        beforeSaveHandleDataComponent.process(context);
        beforeSaveBillHandleBillColumnValueComponent.process(context);
        saveOrUpdateComponent.process(context);
    }

    /**
     * 加载单据
     * @param context
     */
    public void loadBill(LowCodeContext context) {
        billLoadComponent.process(context);
    }

    /**
     * 删除单据
     * @param context
     */
    public void deleteBill(LowCodeContext context) {
    	beforeUpdateValidateStatusComponent.process(context);
        billDeleteComponent.process(context);
    }

    /**
     * 查询
     * @param context
     */
    public void query(LowCodeContext context) {
        beforeActionHandlerQueryWrapperComponent.process(context);
        queryComponent.process(context);
    }

    /**
     * 分页查询
     * @param context
     */
    public void page(LowCodeContext context) {
        beforeActionHandlerQueryWrapperComponent.process(context);
        pageQueryComponent.process(context);
    }

    public void executeSave(LowCodeContext context) {
        dataPreprocessingComponent.process(context);
        saveComponent.process(context);
        updateComponent.process(context);
    }

    public void executeSaveWithFlow(LowCodeContext context) {
        dataPreprocessingComponent.process(context);
        saveComponent.process(context);
        updateComponent.process(context);
        flowStartComponent.process(context);
    }

    public void executeUpdate(LowCodeContext context) {
        dataPreprocessingComponent.process(context);
        updateComponent.process(context);
    }

    public void executeDelete(LowCodeContext context) {
        dataPreprocessingComponent.process(context);
        deleteComponent.process(context);
    }

}
